﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using BaseData;
using BaseData.Common;
using ShiziEntity.User;

namespace ShizirDAL.User
{
    /// <summary>
    /// 经纪人权限控制数据访问类
    /// </summary>
    public class UserAgentConData
    {
        public UserAgentConData()
        {
        }

        /// <summary>
        /// 保存用户权限
        /// </summary>
        /// <param name="user">用户权限实体</param>
        /// <returns>操作结果</returns>
        public bool SaveUserAgentCon(UserAgentCon obj)
        {
            string sql = obj.GetInsertSql();
            int rows = DataConfig.QueryNoRecords(BusinessType.ShiZirWrite, sql);
            return rows > 0;
        }

        /// <summary>
        /// 获取用户权限实体
        /// </summary>
        public UserAgentCon GetAgentConEntity(int userID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1  ");
            strSql.Append(" userID,userName,cityName,houseLimit,houseCurrent,houseMax,houseCount,goodHouseCount,csCurrent,csCount,csGoodCount,csAdd,csMod,csRef,csDel,czCurrent,czCount,czGoodCount,czAdd,czMod,czRef,czDel,lastLoginTime,score ");
            strSql.Append(" from UserAgentCon ");
            strSql.Append(" where userID=" + userID + " ");
            DataTable dt = DataConfig.QueryRecords(BusinessType.ShiZirWrite, strSql.ToString());
            UserAgentCon model = this.GetEntityHelper(dt);
            return model;
        }

        /// <summary>
        /// 从DataTable中得到一个实体对象
        /// </summary>
        private UserAgentCon GetEntityHelper(DataTable dt)
        {
            UserAgentCon model = null;
            if (dt != null && dt.Rows.Count > 0)
            {
                model.UserID = TableHelper.GetIntValueFromDr(dt.Rows[0], "UserID");
                model.UserName = dt.Rows[0]["UserName"].ToString();
                model.CityName = dt.Rows[0]["CityName"].ToString();
                model.HouseLimit = TableHelper.GetIntValueFromDr(dt.Rows[0], "HouseLimit");
                model.HouseCurrent = TableHelper.GetIntValueFromDr(dt.Rows[0], "HouseCurrent");
                model.HouseMax = TableHelper.GetIntValueFromDr(dt.Rows[0], "HouseMax");
                model.HouseCount = TableHelper.GetIntValueFromDr(dt.Rows[0], "HouseCount");
                model.GoodHouseCount = TableHelper.GetIntValueFromDr(dt.Rows[0], "GoodHouseCount");
                model.CsCurrent = TableHelper.GetIntValueFromDr(dt.Rows[0], "CsCurrent");
                model.CsCount = TableHelper.GetIntValueFromDr(dt.Rows[0], "CsCount");
                model.CsGoodCount = TableHelper.GetIntValueFromDr(dt.Rows[0], "CsGoodCount");
                model.CsAdd = TableHelper.GetIntValueFromDr(dt.Rows[0], "CsAdd");
                model.CsMod = TableHelper.GetIntValueFromDr(dt.Rows[0], "CsMod");
                model.CsRef = TableHelper.GetIntValueFromDr(dt.Rows[0], "CsRef");
                model.CsDel = TableHelper.GetIntValueFromDr(dt.Rows[0], "CsDel");
                model.CzCurrent = TableHelper.GetIntValueFromDr(dt.Rows[0], "CzCurrent");
                model.CzCount = TableHelper.GetIntValueFromDr(dt.Rows[0], "CzCount");
                model.CzGoodCount = TableHelper.GetIntValueFromDr(dt.Rows[0], "CzGoodCount");
                model.CzAdd = TableHelper.GetIntValueFromDr(dt.Rows[0], "CzAdd");
                model.CzMod = TableHelper.GetIntValueFromDr(dt.Rows[0], "CzMod");
                model.CzRef = TableHelper.GetIntValueFromDr(dt.Rows[0], "CzRef");
                model.CzDel = TableHelper.GetIntValueFromDr(dt.Rows[0], "CzDel");
                model.LastLoginTime = TableHelper.GetDateTimeValueFromDr(dt.Rows[0], "LastLoginTime");
                model.Score = TableHelper.GetIntValueFromDr(dt.Rows[0], "Score");
            }
            return model;
        }
    }
}